package com.qqtech.qquan.circle.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.admin.context.AdminPage;
import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.circle.enums.CircleTopicOptTypeEnum;
import com.qqtech.qquan.circle.enums.CircleTopicStatusDictEnum;
import com.qqtech.qquan.circle.model.CircleBaseVo;
import com.qqtech.qquan.circle.model.CircleTopic;
import com.qqtech.qquan.circle.model.CircleTopicQuery;
import com.qqtech.qquan.circle.model.CircleTopicVo;
import com.qqtech.qquan.circle.service.CircleBaseService;
import com.qqtech.qquan.circle.service.CircleTopicService;
import com.qqtech.qquan.sys.controller.BaseAdminController;
import com.qqtech.qquan.user.model.UserBaseVo;
import com.qqtech.qquan.user.service.UserBaseService;

@Controller
@RequestMapping(value = "/circle/topic")
public class CircleTopicController extends BaseAdminController {
	@Resource
	private CircleTopicService circleTopicService;
	@Resource
	private CircleBaseService circleBaseService;

	@Resource
	private UserBaseService userBaseService;

	@Override
	protected BaseService<CircleTopic> getBaseService() {
		return circleTopicService;
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list() {
		return "/circle/circleTopic-list";
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage list(CircleTopicQuery query, HttpServletRequest request, HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		String rows = (String) request.getParameter("rows"); // 每页条数
		query.setSize(Integer.valueOf(rows));
		query.setStatus(CircleTopicStatusDictEnum.NORMAL.getCode());
		Pageable pageKit = new PageKit(query.getPage(), query.getSize(), new PageOrder(Direction.DESC, "newAt"));
		Page<CircleTopicVo> page = circleTopicService.queryPageListAdmin(query, pageKit);
		adminPage.setRows(page.getContent());
		adminPage.setTotal(Integer.valueOf(page.getTotalElements() + ""));
		return adminPage;
	}

	@RequestMapping(value = "/detail")
	public String detail(Integer id, Model model) {
		CircleTopicVo topicVo = circleTopicService.getById(id);
		UserBaseVo userBaseVo = userBaseService.getById(topicVo.getCreatorId());
		if(userBaseVo!=null){
			topicVo.setCreatorName(userBaseVo.getNick());
		}
		CircleBaseVo circleVo = circleBaseService.getById(topicVo.getCircleId());
		if(circleVo!=null){
			topicVo.setCircleName(circleVo.getName());
		}
		model.addAttribute("topicVo", topicVo);
		return "/circle/circleTopic-detail";
	}

	/**
	 * 话题置顶
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/doTop4Topic")
	@ResponseBody
	public Result doTop4Topic(Integer id) {
		Result ret = new Result();
		CircleTopic topic=new CircleTopic();
		topic.setId(id);
		topic.setIsTop(YesNoEnum.YES.getCode());
		topic.setTopAt(TimeUtil.now());
		topic.setOptType(CircleTopicOptTypeEnum.TOP.getCode());
		topic.setOptAt(TimeUtil.now());
		int updateCount = circleTopicService.updateById(topic);
		if(updateCount!=1){
			ret.addErr("话题置顶失败");
		}
		return ret;
	}
	
	/**
	 * 话题取消置顶
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/undoTop4Topic")
	@ResponseBody
	public Result undoTop4Topic(Integer id) {
		Result ret = new Result();
		CircleTopic topic=new CircleTopic();
		topic.setId(id);
		topic.setIsTop(YesNoEnum.NO.getCode());
		topic.setTopAt(TimeUtil.now());
		topic.setOptType(CircleTopicOptTypeEnum.TOP.getCode());
		topic.setOptAt(TimeUtil.now());
		int updateCount = circleTopicService.updateById(topic);
		if(updateCount!=1){
			ret.addErr("话题取消置顶失败");
		}
		return ret;
	}
	
	/**
	 * 显示话题（取消屏蔽）
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/showTopic")
	@ResponseBody
	public Result showTopic(Integer id) {
		Result ret = new Result();
		CircleTopic topic=new CircleTopic();
		topic.setId(id);
		topic.setIsShow(YesNoEnum.YES.getCode());
		topic.setOptType(CircleTopicOptTypeEnum.HIDE.getCode());
		topic.setOptAt(TimeUtil.now());
		int updateCount = circleTopicService.updateById(topic);
		if(updateCount!=1){
			ret.addErr("显示话题失败");
		}
		return ret;
	}
	
	/**
	 * 屏蔽话题
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/hideTopic")
	@ResponseBody
	public Result hideTopic(Integer id) {
		Result ret = new Result();
		CircleTopic topic=new CircleTopic();
		topic.setId(id);
		topic.setIsShow(YesNoEnum.NO.getCode());
		topic.setOptType(CircleTopicOptTypeEnum.HIDE.getCode());
		topic.setOptAt(TimeUtil.now());
		int updateCount = circleTopicService.updateById(topic);
		if(updateCount!=1){
			ret.addErr("屏蔽置顶失败");
		}
		return ret;
	}
}
